import json
import re

import requests
import pinyin


path = "stockList.json"
url = "http://1.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112407533719041969591_1611664405306&pn=%s&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1611664405338"


def spiderStock(page, url):
    header = {}
    data = {}
    response = requests.get(url % page, header)
    reRes = re.findall(re.compile("[(](.*)[)]"), response.text)
    return json.loads(reRes[0])


def stock():
    tmpList = []
    count = 0
    for i in range(1, 234):
        res = spiderStock(i, url)
        stockNameList = res["data"]["diff"]
        print(len(stockNameList))
        for name in stockNameList:
            tmpDict = {}
            stockName = name["f14"]
            tmpStr = ""
            for strs in stockName:
                tmpStr += getStrAllAplha(strs)
            
            count += 1
            tmpDict["alpha"] = tmpStr
            tmpDict["name"] = name["f14"]
            tmpDict["code"] = name["f12"]
            if str(name["f12"])[0] == '6':
                tmpDict["prefix"] = "sh"
            elif str(name["f12"])[0] == '0':
                tmpDict["prefix"] = "sz"
            else:
                tmpDict["prefix"] = "kc"
            print(stockName, tmpStr, count, tmpDict["prefix"])
            tmpList.append(tmpDict)
    return tmpList


def getStrAllAplha(strs):
    return pinyin.get_initial(strs).upper()


def saveStockList(stockJson, path):
    with open(path, 'w') as f:
        json.dump(stockJson, f)
    # with open('stocks.txt', 'w') as f:
    #     f.write(str(stockJson))


if __name__ == '__main__':
    stockJson = stock()
    saveStockList(stockJson, path)